namespace SimpleWebServices.Security.Cryptography
{
    using System;
    using System.Xml.Serialization;
    using SimpleWebServices;
    using Xunit;

    public sealed class SecureHashPasswordFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<SecureHashPassword>()
                .DerivesFrom<ValueObject<SecureHashPassword>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("password")
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new SecureHashPassword());
        }

        [Fact]
        public void prop_Hash()
        {
            Assert.True(new PropertyExpectations(typeof(SecureHashPassword).GetProperty("Hash"))
                .XmlAttribute("hash")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Salt()
        {
            Assert.True(new PropertyExpectations(typeof(SecureHashPassword).GetProperty("Salt"))
                .XmlAttribute("salt")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void op_Compute_string()
        {
            SecureHashPassword obj = SecureHashPassword.Compute("password");

            Assert.True(!string.IsNullOrEmpty(obj.Salt));
            Assert.True(!string.IsNullOrEmpty(obj.Hash));
        }

        [Fact]
        public void op_Compute_stringNull()
        {
            Assert.Throws<ArgumentNullException>(() => SecureHashPassword.Compute(null));
        }

        [Fact]
        public void op_Compute_stringEmpty()
        {
            SecureHashPassword obj = SecureHashPassword.Compute(string.Empty);

            Assert.True(!string.IsNullOrEmpty(obj.Salt));
            Assert.True(!string.IsNullOrEmpty(obj.Hash));
        }

        [Fact]
        public void op_Compute_stringNull_Salt()
        {
            Assert.Throws<ArgumentNullException>(() => SecureHashPassword.Compute(null, new Salt()));
        }

        [Fact]
        public void op_Compute_stringEmpty_Salt()
        {
            SecureHashPassword obj = SecureHashPassword.Compute(string.Empty, new Salt());

            Assert.True(!string.IsNullOrEmpty(obj.Salt));
            Assert.True(!string.IsNullOrEmpty(obj.Hash));
        }

        [Fact]
        public void op_Compute_string_SaltNull()
        {
            Assert.Throws<ArgumentNullException>(() => SecureHashPassword.Compute("password", null));
        }

        [Fact]
        public void op_Compute_string_Salt()
        {
            SecureHashPassword password1 = SecureHashPassword.Compute("password");
            SecureHashPassword password2 = SecureHashPassword.Compute("password", password1.Salt);

            Assert.Equal<string>(password1.Hash, password2.Hash);
        }

        [Fact]
        public void op_Authenticate_stringMatch()
        {
            SecureHashPassword obj = SecureHashPassword.Compute("password");

            Assert.True(obj.Authenticate("password"));
        }

        [Fact]
        public void op_Authenticate_stringMismatch()
        {
            SecureHashPassword obj = SecureHashPassword.Compute("foo");

            Assert.False(obj.Authenticate("bar"));
        }

        [Fact]
        public void op_Authenticate_stringEmpty()
        {
            SecureHashPassword obj = SecureHashPassword.Compute(string.Empty);

            Assert.True(obj.Authenticate(string.Empty));
        }

        [Fact]
        public void op_Authenticate_stringNull()
        {
            Assert.Throws<ArgumentNullException>(() => SecureHashPassword.Compute("password").Authenticate(null as string));
        }
    }
}